package com.junyidao.game.controller;

import com.junyidao.game.constant.OperLogConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.core.annotation.OperLog;
import com.junyidao.game.pojo.PCode;
import com.junyidao.game.pojo.SysUser;
import com.junyidao.game.pojo.TChannel;
import com.junyidao.game.pojo.TMsgSending;
import com.junyidao.game.service.ChannelService;
import com.junyidao.game.service.CodeService;
import com.junyidao.game.service.MsgSendedService;
import com.junyidao.game.service.MsgSendingService;
import com.junyidao.game.vo.MsgSendedVo;
import com.junyidao.game.vo.MsgSendingVo;
import com.junyidao.game.vo.TChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/7/14.
 */
@Controller
@RequestMapping("/messages")
public class MessagesController extends BaseController {

    @Autowired
    private CodeService codeService;

    @Autowired
    private MsgSendingService msgSendingService;

    @Autowired
    private MsgSendedService msgSendedService;

    @RequestMapping
    @Action(desc = "消息发布页面")
    public ModelAndView list() {
        ModelAndView result = new ModelAndView("messages/list");
        List<PCode> messagesType = codeService.select("MessagesType");
        result.addObject("messagesType", messagesType);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/listSending")
    @Action(desc = "获取待发送消息", sysPriv = false)
    public Map<String, Object> listSending(MsgSendingVo vo) {
        Map<String, Object> ret = msgSendingService.getMsgSendDingOrSave(vo);
        return resultSucess(ret);
    }

    @ResponseBody
    @RequestMapping(value = "/listSave")
    @Action(desc = "获取暂存消息", sysPriv = false)
    public Map<String, Object> listSave(MsgSendingVo vo) {
        Map<String, Object> ret = msgSendingService.getMsgSendDingOrSave(vo);
        return resultSucess(ret);
    }

    @ResponseBody
    @RequestMapping(value = "/listRelease")
    @Action(desc = "获取已发送消息", sysPriv = false)
    public Map<String, Object> listRelease(MsgSendedVo vo) {
        Map<String, Object> ret = msgSendedService.getSendedMsg(vo);
        return resultSucess(ret);
    }

    @ResponseBody
    @RequestMapping(value = "/delSending")
    @Action(operType = OperLogConstant.MESSAGES_DEL_SENDING, desc = "删除待发送或暂存消息", needLog = true)
    public Map<String, Object> delSending(String ids) {
        msgSendingService.delSending(ids);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/delSended")
    @Action(operType = OperLogConstant.MESSAGES_DEL_SENDED, desc = "删除已发送消息", needLog = true)
    public Map<String, Object> delSended(String ids) {
        msgSendedService.delSended(ids);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/addMsg")
    @Action(operType = OperLogConstant.MESSAGES_ADD, desc = "新增消息", needLog = true)
    public Map<String, Object> addMsg(TMsgSending msg) {
        msgSendingService.addSendingMsg(msg);
        return resultSucess(null);
    }


    @RequestMapping(value = "releaseMsg")
    @Action(desc = "跳转到消息发布页面")
    public ModelAndView releaseMsg(String ids) {
        ModelAndView result = new ModelAndView("messages/release");
        List<MsgSendingVo> list = msgSendingService.getMsgSending(ids);
        StringBuffer sb = new StringBuffer();
        if (!StringUtils.isEmpty(ids)) {
            for (int i = 0; i < list.size(); i++) {
                if (list.size() > 1) {
                    sb.append(i + 1);
                    sb.append("、");
                }
                sb.append(list.get(i).getMsgContent());
                sb.append("\n");
            }
        }
        List<PCode> messagesType = codeService.select("MessagesType");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = sdf.format(new Date());
        result.addObject("messagesType", messagesType);
        result.addObject("msgContent", sb.toString());
        result.addObject("date", date);
        result.addObject("ids", ids);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/addSendedMsg")
    @Action(operType = OperLogConstant.MESSAGES_RELEASE, desc = "发布消息", needLog = true)
    public Map<String, Object> addSendedMsg(MsgSendedVo vo, String ids, String uids, String channelIds, String groupIds) throws Exception {
        msgSendedService.addSendedMsg(vo, ids, uids, channelIds, groupIds);
        return resultSucess(null);
    }

}
